
import requests
from APIs.baseAPI import BaseAPI
from APIs.loginAPI import LoginAPI
from common.config import ip


class LogsAPI(BaseAPI):
    def uploadFile(self):
        # 文件上传
        # 普通键值对
        fileData = {"type": "img"}

        # 定义文件参数：'message_img'键来自抓包时的name对应的值，
        # 值是通过数组的形式传值，第一个参数是抓包时的'filename'对应的值，
        #                      第二个参数是通过open打开一个文件，且是二进制读的形式，
        #      第三个参数是抓包时的文件类型
        furl = r"D:\work\softp\python\APIAuto\datas\1.png"
        fileD = {"file": (
            "1.png",
            open(furl, "rb"),
            "image/jpeg")
        }
        mydata = {"username": "admin", "password": "123456"}
        r = requests.post(f"{ip}/file/upload", data=fileData, headers=self.header, files=fileD)
        print(r.json())
    def addLog(self,code):
        # code = {
        #     "categoryId": categoryId,
        #     "content": content,
        #     "tomorrow": tomorrow,
        #     "question": question,
        #     "batchId": batchId,
        #     "sendUserIds": sendUserIds,
        #     "sendDeptIds": sendDeptIds,
        #     "customerIds": customerIds,
        #     "contactsIds": contactsIds,
        #     "businessIds": businessIds,
        #     "contractIds": contractIds
            # "categoryId": "1",
            # "content": "这里是今日内容的修改~~",
            # "tomorrow": "这里明日工作计划的修改~",
            # "question": "这里是今日遇到的问题的修改",
            # "batchId": "a6522a16cc407ef091adb0b1a9a93949",
            # "sendUserIds": "3,4",
            # "sendDeptIds": "",
            # "customerIds": "",
            # "contactsIds": "",
            # "businessIds": "",
            # "contractIds": ""
        # }
        r = requests.post(f"{ip}/OaLog/addOrUpdate", json=code,headers=self.header)
        # t=r.json()[0]["code"]
        return r.json()

    def getLog(self,code):
        # 获取日志
        # code = {
        #     "page": 1,
        #     "limit": 5,
        #     "by": "1"
        # }
        r = requests.post(f"{ip}/OaLog/queryList", json=code, headers=self.header)
        return r.json()
        # rList=r.json()["data"]["list"]
        # ls=[]
        # for i in rList:
        #     ls.append(i["logId"])
        # print(ls)
        # 取第一个logid
        # id = r.json()["data"]["list"][0]["logId"]
        # return id
    # def getLogId(self,code):
    #     code = {
    #         "page": 1,
    #         "limit": 5,
    #         "by": "1"
    #     }
    #     r = requests.post(f"{ip}/OaLog/queryList", json=code, headers=self.header)
    #     # rList=r.json()["data"]["list"]
    #     # ls=[]
    #     # for i in rList:
    #     #     ls.append(i["logId"])
    #     # print(ls)
    #     #取第一个logid
    #     id = r.json()["data"]["list"][0]["logId"]
    #     return id
    def updateLog(self,code):
        # 修改日志
        # code = {
        #     "logId": 5,
        #     "categoryId": "1",
        #     "content": "11111111111",
        #     "tomorrow": "aaaaaaaaaaaaaaaadddddddddd",
        #     "question": "11111111111",
        #     "batchId": "09c2766c7f1afa2c037d156497cc1f6e",
        #     "sendUserIds": "",
        #     "sendDeptIds": "",
        #     "customerIds": "",
        #     "contactsIds": "",
        #     "businessIds": "",
        #     "contractIds": ""
        # }
        r = requests.post(f"{ip}/OaLog/addOrUpdate", json=code, headers=self.header)
        return r.json()
    def deleteLog(self):
        code = {
            "page": 1,
            "limit": 5,
            "by": "1"
        }
        r = requests.post(f"{ip}/OaLog/queryList", json=code, headers=self.header)
        # rList=r.json()["data"]["list"]
        # ls=[]
        # for i in rList:
        #     ls.append(i["logId"])
        # print(ls)
        # 取第一个logid
        id = r.json()["data"]["list"][0]["logId"]
        mydata= {"logId": id}
        r = requests.post(f"{ip}/OaLog/deleteById", data=mydata, headers=self.header)
        return r.json()
if __name__ == '__main__':
    l=LoginAPI()
    s=l.getToken()
    a=LogsAPI(s)
    #新增日志
    # a.addLog(
    #     {"categoryId": "1",
    #         "content": "这里是今日内容的修改~~",
    #         "tomorrow": "这里明日工作计划的修改~",
    #         "question": "这里是今日遇到的问题的修改",
    #         "batchId": "a6522a16cc407ef091adb0b1a9a93949",
    #         "sendUserIds": "3,4",
    #         "sendDeptIds": "",
    #         "customerIds": "",
    #         "contactsIds": "",
    #         "businessIds": "",
    #          "contractIds": ""
    #      })
    # #获取日志
    # a.getLog()
    # #修改
    # a.updateLog()
    # #删除
    # a.deleteLog()